/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  TouchableHighlight,
  NativeModules
} from 'react-native';

class CustomButton extends React.Component {
  render() {
    return (
      <TouchableHighlight
        style={styles.button}
        underlayColor="#a5a5a5"
        onPress={this.props.onPress}>
        <Text style={styles.buttonText}>{this.props.text}</Text>
      </TouchableHighlight>
    );
  }
}

class reactnativeBluetoothDemo extends Component {
  _onpressInit=()=>{
    NativeModules.BluetoothAndroid.enable();//打开蓝牙，然后需要手工配对，没有做搜索
    NativeModules.BluetoothAndroid.getBondedDevices();//获取已经配对的设备
    NativeModules.BluetoothAndroid.socketconnet();//用匹配到的蓝牙设备作为serve端进行socket连接
    //console.log(NativeModules);
  };

  _onpressSend=()=>{
    var bytes=[0x11,0x22,0x33,0x44,0x55,0x66,0x77];
    NativeModules.BluetoothAndroid.socketwrite(bytes);//发送一个数据
  };
  _onpressClose=()=>{
    NativeModules.BluetoothAndroid.socketclose();//发送一个数据
  };
  render() {
    return (
      <View>
        <Text style={styles.welcome}>
          蓝牙测试
        </Text>
        <CustomButton
          text="蓝牙初始化"
          onPress={this._onpressInit}
        />
        <CustomButton
          text="发送一个数据"
          onPress={this._onpressSend}
        />
        <CustomButton
          text="关闭socket"
          onPress={this._onpressClose}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
   button: {
    margin:5,
    backgroundColor: 'white',
    padding: 15,
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderBottomColor: '#cdcdcd',
  },
});

AppRegistry.registerComponent('reactnativeBluetoothDemo', () => reactnativeBluetoothDemo);
